# coding: utf-8
from qfluentwidgets import SettingCardGroup, SwitchSettingCard, OptionsSettingCard, \
    CustomColorSettingCard, FluentIcon as FIF, InfoBar, setTheme, setThemeColor

from common import cfg, isWin11, signalBus
from components import VerticalScrollArea


class ThemeInterface(VerticalScrollArea):

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.personalGroup = SettingCardGroup(self.tr('个性化'), self)
        self.micaCard = SwitchSettingCard(
            FIF.TRANSPARENT,
            self.tr('云母效果'),
            '',
            cfg.micaEnabled,
            self.personalGroup
        )
        self.themeCard = OptionsSettingCard(
            cfg.themeMode,
            FIF.BRUSH,
            self.tr('主题'),
            '',
            texts=[
                self.tr('明亮'), self.tr('黑暗'),
                self.tr('跟随系统')
            ],
            parent=self.personalGroup
        )
        self.themeColorCard = CustomColorSettingCard(
            cfg.themeColor,
            FIF.PALETTE,
            self.tr('主题颜色'),
            '',
            self.personalGroup
        )
        self.zoomCard = OptionsSettingCard(
            cfg.dpiScale,
            FIF.ZOOM,
            self.tr("界面缩放"),
            '',
            texts=[
                "100%", "125%", "150%", "175%", "200%",
                self.tr("跟随系统")
            ],
            parent=self.personalGroup
        )
        self.languageCard = OptionsSettingCard(
            cfg.language,
            FIF.LANGUAGE,
            self.tr('语言'),
            '',
            texts=['简体中文', '繁體中文', 'English', self.tr('跟随系统')],
            parent=self.personalGroup
        )
        self.__initWidgets()
        self.__connectSignalToSlot()

    def __initWidgets(self):
        self.setObjectName('ThemeInterface')
        self.micaCard.setEnabled(isWin11())

        self.personalGroup.addSettingCards(
            [
                self.micaCard,
                self.themeCard,
                self.themeColorCard,
                self.zoomCard,
                self.languageCard
            ]
        )
        self.setContentsMargins(0, 0, 0, 0)
        self.addWidget(self.personalGroup)

    def __showRestartTooltip(self):
        """ show restart tooltip """
        InfoBar.success(
            self.tr('更新成功'),
            self.tr('重启后配置生效'),
            duration=1500,
            parent=self
        )

    def __connectSignalToSlot(self):
        """ connect signal to slot """
        cfg.appRestartSig.connect(self.__showRestartTooltip)

        # personalization
        cfg.themeChanged.connect(setTheme)
        self.themeColorCard.colorChanged.connect(lambda c: setThemeColor(c))
        self.micaCard.checkedChanged.connect(signalBus.micaEnableChanged)
